package health.lm.com.component_base.util;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2019/1/5
 * 邮箱：15206394364@163.com
 * 介绍：倒计时工具类
 * 修订：====================
 */
public class TimerHelper {
    private static TimerHelper timerHelper;
    private CountDownTimer timer;

    public TimerHelper() {
    }

    public synchronized static TimerHelper getInstance() {
        if (timerHelper == null) timerHelper = new TimerHelper();
        return timerHelper;
    }

    public String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        String downTime;
        if (hours > 24)
            downTime = hours / 24 + "天\t" + hours % 24 + ":";
        else if (10 < hours && hours < 24) downTime = hours + ":";
        else downTime = "0" + hours + ":";
        if (minutes < 10) downTime = downTime + "0" + minutes + ":";
        else downTime = downTime + minutes + ":";
        if (seconds < 10) downTime = downTime + "0" + seconds;
        else downTime += seconds;
        return downTime;
    }

    /**
     * 文字
     *
     * @param time 时间戳
     * @return 日时分格式
     */
    public String generateTextTime(long time) {
        if (time == 0) return "0秒";
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        String downTime;
        if (hours > 24)
            downTime = hours / 24 + "天\t" + hours % 24 + "时:";
        else if (10 < hours && hours < 24) downTime = hours + "时:";
        else downTime = "0" + hours + "时:";
        if (minutes < 10) downTime = downTime + "0" + minutes + "分:";
        else downTime = downTime + minutes + "分:";
        if (seconds < 10) downTime = downTime + "0" + seconds + "秒";
        else downTime = downTime + seconds + "秒";
        return downTime;
    }

    public CountDownTimer onTimer(String time, TimerHelpCall helpCall) {
        if (TextUtils.isEmpty(time)) return null;
        return onTimer(Long.parseLong(time), helpCall);
    }

    @SuppressLint("SimpleDateFormat")
    public CountDownTimer onTimer(long time, TimerHelpCall helpCall) {
        if (time <= 0) return null;
        if (null == helpCall) return null;
        //时间差值
        long diff = (time) * 1000;
        if (diff <= 0) {
            helpCall.finish();
            return null;
        }

        //初始化Formatter的转换格式。
        timer = new CountDownTimer(diff, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
                //formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
                //String hms = formatter.format(millisUntilFinished);
                helpCall.started(generateTime(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                helpCall.finish();
            }
        };
        timer.start();
        helpCall.setCancel(timer);
        return timer;
    }

    /**
     * 带汉字的日时分秒
     *
     * @param time     时间戳
     * @param helpCall 回调
     */
    @SuppressLint("SimpleDateFormat")
    public void onTimer2(long time, TimerHelpCall helpCall) {
        if (time < 0) return;
        if (null == helpCall) return;
        //时间差值
        long diff = (time) * 1000;
        if (diff < 0) {
            helpCall.finish();
            return;
        }

        timer = new CountDownTimer(diff, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
                //formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
                //String hms = formatter.format(millisUntilFinished);
                helpCall.started(generateTextTime(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                helpCall.finish();
            }
        };
        timer.start();
        helpCall.setCancel(timer);
    }

    /**
     * 倒计时
     *
     * @param textView 控件
     * @param waitTime 倒计时总时长
     * @param interval 倒计时的间隔时间
     * @param hint     倒计时完毕时显示的文字
     */
    public static CountDownTimer countDown(final TextView textView, long waitTime, long interval, final String hint) {
        textView.setEnabled(false);
        android.os.CountDownTimer timer = new android.os.CountDownTimer(waitTime, interval) {

            @SuppressLint("DefaultLocale")
            @Override
            public void onTick(long millisUntilFinished) {//重新发送(60)s
                textView.setText(String.format("重新发送 (%d) s", millisUntilFinished / 1000));
            }

            @Override
            public void onFinish() {
                textView.setEnabled(true);
                textView.setText(hint);
            }
        };
        timer.start();
        return timer;
    }

    public interface TimerHelpCall {
        /*开始计时*/
        void started(String time);

        /*停止计时，节省内存*/
        void setCancel(CountDownTimer downTimer);

        /*倒计时结束*/
        void finish();
    }
}
